package sysAuth

import initConfig.SysInitData
import sysUtil.SysCommUtil

class SysNotice {
    String title;
    String content;
    String type = "0" //通知类型：0，public ；1，private
    String toUser ; //通知类型如果为 private ，则该字段必填:用户ID 消息 给谁
    int remainDay = 1//有效时长 ：天
    String state = "1" //是否有已读：1 否，0.是
    String delFlag = "0" //是否删除:0 否，1 是
    Long parentId ; //回复notice

    String creator ;//操作人
    String updater;//最近一次更新人

    Date dateCreated;
    Date lastUpdated;

    def sysDBUtilService
    static constraints = {
        title(nullable: false)
        content(nullable: false,maxSize: 3000)
        type(nullable: false)
        state(nullable: false)
        toUser(nullable: true)
        remainDay(nullable: false)
        parentId(nullable: true)

        creator(nullable: false)
        updater(nullable: true)
    }

    static mapping = {
        version false
    }

    /**
     * 更新公告
     * @return
     */
    public updateState(){
        Date date = new Date()
        def now = date.getTime()
        def createDate = this.getDateCreated().getTime() + Long.parseLong(String.valueOf(this.remainDay?  1000*86400 * this.remainDay : 0))
        if( createDate < now){
            def db = sysDBUtilService.getGroovySqlInstance()
            db.executeUpdate("UPDATE  sys_notice SET state = 0 WHERE id = ${this.id} ")
        }
    }
}
